﻿Public Class FormPurchase

    Private Sub FormPurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)
        LoadData()

    End Sub

    Public Sub LoadData()
        Dim sql As String = "select p.PurchaseId,p.PurchaseNo,p.PurchaseDate,p.Subtotal,p.Discount,p.Total,v.VendorName,s.StoreName from Purchase p INNER JOIN Vendor v on v.VendorId=p.VendorId INNER JOIN Store s on s.StoreId=p.StoreId WHERE p.Active=1 AND  p.PurchaseDate BETWEEN '" & Me.DateTimePicker1.Value.ToString("yyyy-MM-dd") & "' AND '" & Me.DateTimePicker2.Value.ToString("yyyy-MM-dd") & "'"
        Me.dgv.DataSource = Cmd.ExecuteDataTable(sql)
    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        LoadData()

    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim frm As New FormPurchaseEditor()
        frm.ShowDialog()

    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

  
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Cmd.Begin()
        Try
            Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
            Dim id = row.Cells("PurchaseId").Value
            Dim storeId = Cmd.ExecuteScalar("SELECT StoreId FROM Purchase WHERE PurchaseId=" & id)
            Cmd.ExecuteNonQuery("UPDATE Purchase SET Active=0 WHERE PurchaseId=" & id)
            Dim dtPurchaseDetail As DataTable = Cmd.ExecuteDataTable("SELECT * FROM PurchaseDetail WHERE Active=1 AND  PurchaseId = " & id)
            For Each r As DataRow In dtPurchaseDetail.Rows
                Cmd.ExecuteNonQuery("UPDATE Stock SET Qty = Qty - " & r("Qty") & " WHERE ProductId=" & r("ProductId") & " AND StoreId = " & storeId)
                Cmd.ExecuteNonQuery("UPDATE PurchaseDetail SET Active=0 WHERE PurchaseDetailId=" & r("PurchaseDetailId"))
            Next

            Cmd.Commit()
            LoadData()

        Catch ex As Exception
            Cmd.Rollback()
            MsgBox(ex.Message)

        End Try
        
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        Cmd.Parameters("@search") = "%" & txtsearch.Text & "%"
        dgv.DataSource = Cmd.ExecuteDataTable("select p.PurchaseId,p.PurchaseNo,p.PurchaseDate,p.Subtotal,p.Discount,p.Total,v.VendorName,s.StoreName from Purchase p INNER JOIN Vendor v on v.VendorId=p.VendorId INNER JOIN Store s on s.StoreId=p.StoreId Where PurchaseNo like @search")
    End Sub
End Class